package com.example.demo.controller;

import com.example.comon.domain.R;
import com.example.demo.domain.Bo.UserRoleListBo;
import com.example.demo.domain.Role;
import com.example.demo.domain.UserRole;
import com.example.demo.domain.Vo.UserRoleListVo;
import com.example.demo.service.IUserRoleService;
import com.example.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/userRole")
@Api(value = "userRole",tags = "用户角色控制器")
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;

    @GetMapping("/getUserRole")
    @ApiOperation("获取角色菜单")
    public R<List<UserRoleListVo>> getUserRoleList(UserRoleListBo bo) {
        UserRole userRole = new UserRole();
        Long roleId = bo.getRoleId();
        Long userId = bo.getUserId();
        userRole.setUserId(bo.getUserId());
        List<UserRoleListVo> list = (List<UserRoleListVo>) userRoleService.selectByUserIdAndRoleId(roleId, userId);
        return R.success(list);
    }


    @PostMapping("/addUserRole")
    @ApiOperation("增加用户角色")
    public R addUserRole(@RequestBody UserRoleListBo bo) {
        UserRole userRoleRes = userRoleService.selectByUserIdAndRoleId(bo.getUserId(),bo.getRoleId());
        if(userRoleRes !=null){
            return R.error("该用户已授权该角色，请勿重复授权");
        }

        UserRole userRole = new UserRole();
        userRole.setRoleId(bo.getRoleId());
        userRole.setUserId(bo.getUserId());
        userRole.setCreateBy(1111L);
        userRole.setUpdateBy(2222L);
        userRole.setCreateTime(new Date());
        userRole.setUpdateTime(new Date());
        userRoleService.insertUserRole(userRole);
        return R.success("授权成功");
    }

    /**
     * 查询用户的角色（用户有哪些角色）
     * @param userId
     * @return
     */
    @PostMapping("/getRoleByUserId")
    @ApiOperation("查询用户有那些角色")
    public R getRoleByUserId(Long userId) {
        List<Role> list = userRoleService.getRoleByUserId(userId);
        return R.success( list);
    }


}
